package com.qf.spring2202.springmybatis.service.impl;

import com.qf.spring2202.springmybatis.dao.UserDao;
import com.qf.spring2202.springmybatis.entity.User;
import com.qf.spring2202.springmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
@Service
public class UserServiceImpl implements UserService {
@Autowired
    private UserDao userDao;

    /*public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }*/

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public User getUser(Integer id) {

        System.out.println("UserServiceImpl 查询用户id="+id);

        System.out.println(userDao.getUserById(id));

        System.out.println(userDao.getUserById(30));

        return userDao.getUserById(id);
    }

    @Override
    public Integer createUser(User user) {
        userDao.insertUser(user);
        int a = 1/0;
        userDao.insertUser(user);
        return 2;
    }

    @Override
    public void sleep() {
        userDao.sleep();
    }

    /**
     * 测试当发生 CheckException 类的异常时，会不会回顾
     */
    @Override
    public void testExRollback(User user) throws SQLException {


        userDao.insertUser(user);

        if (true) { // 骗过编译器
            throw new SQLException();
        }

        userDao.insertUser(user);


    }


}
